package com.ssm.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.po.Category;
import com.ssm.service.CategoryService;

@Controller
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	// 查看分类
	@RequestMapping(value = "/findCategoryList.action")
	public String findCategoryList(Model model) {
		//获取分类列表
		List<Category> categoryList = categoryService.findCategoryList();
		model.addAttribute("categoryList", categoryList);
		return "category/category_list";
	}
	
	//跳转增加/修改分类
	@RequestMapping(value="/toAddCategory.action")
	public String toAddCategory(Category category,Model model){
		category = categoryService.findCategoryById(category.getCategoryId());
		if(category==null) {
			model.addAttribute("type","增加分类");
		}else {
			model.addAttribute("type","修改分类");
			model.addAttribute("category",category);
		}
		
		return "category/add_category";
	}
	
	//增加分类
	@RequestMapping(value="/addCategory.action")
	public String AddCategory(Category category,Model model){
		categoryService.addCategory(category);
		return "redirect:findCategoryList.action";
	}
	
	//修改分类
	@RequestMapping(value="/editCategory.action")
	public String editCategory(Category category,Model model){
		categoryService.updateCategory(category);
		return "redirect:findCategoryList.action";
	}
	
	//删除分类
	@RequestMapping(value="/delCategory.action")
	@ResponseBody
	public int delCategory(@RequestBody Category category,Model model){
		System.out.println(category);
		int categoryId=category.getCategoryId();
		int i=categoryService.delCategory(categoryId);
		if(i>0) {
			return categoryId;
		}else {
			return 0;
		}
	}
	
	//更新首页导航
	@RequestMapping(value="/setCategoryHome.action")
	public String setCategoryHome(Category category,Model model){
		categoryService.setCategoryHome(category);
		return "redirect:findCategoryList.action";
	}
}
